﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.VoteManage.Controls
{
	public partial class Vote : Hxsoft.AppV1.UserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
				this.DataBind();
		}

		protected override void OnInit(EventArgs e)
		{
			this.CBLChannelIds.DataBind();
			base.OnInit(e);
		}

		protected override void OnPreRender(EventArgs e)
		{
			ApplicationPage.RegisterHeadIncludeFile(ApplicationConfiguration.JQueryUrl, IncludeFileType.JS);

			string script = "";
			foreach (var voteItem in VoteItemList)
			{
				script += this.ClientID + @"_AddVoteItem(""" + voteItem.OptionTitle.ToJavascriptString() + @""", " +
					@"""" + voteItem.VoteCount + @"""" +
					@");";
			}
			ScriptManager.RegisterStartupScript(this, this.GetType(), "_InitVoteItemList", script, true);

			base.OnPreRender(e);
			FCKeditor.BindUploadUrl(CtrlContent, GlobalId, Recruitments.INFO_TYPE, SiteId);
		}
		private LoadedValue<int?> loadedSiteId;
		public int? SiteId
		{
			get
			{
				if (loadedSiteId.Loader == null)
					loadedSiteId.Loader = () =>
					{
						int? currentSiteId = Sites.CurrentSiteId;
						if (!currentSiteId.HasValue) currentSiteId = IntExtensions.ParseOrNull(Request.QueryString["SiteId"]);
						return currentSiteId;
					};
				return loadedSiteId.Value;
			}
		}
		/// <summary>
		/// 全局标识
		/// </summary>
		public Guid GlobalId
		{
			get
			{
				if (ViewState["GlobalId"] == null)
					ViewState["GlobalId"] = Guid.NewGuid();
				return (Guid)ViewState["GlobalId"];
			}
			set
			{
				ViewState["GlobalId"] = value;
			}
		}
		#region 属性
		/// <summary>
		/// 属性 Title
		/// </summary>
		public string Title
		{
			get
			{
				return Convert.ToString(CtrlTitle.Text);
			}
			set
			{
				CtrlTitle.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 Summary
		/// </summary>
		public string Summary
		{
			get
			{
				return CtrlSummary.Text;
			}
			set
			{
				CtrlSummary.Text = value;
			}
		}

		/// <summary>
		/// 属性 Content
		/// </summary>
		public string Content
		{
			get
			{
				if (String.IsNullOrEmpty(CtrlContent.Value))
					return null;

				return Convert.ToString(CtrlContent.Value);
			}
			set
			{
				if (value == null)
					CtrlContent.Value = "";
				else
					CtrlContent.Value = value.ToString();
			}
		}

		/// <summary>
		/// 属性 ReleaseDate
		/// </summary>
		public DateTime? ReleaseDate
		{
			get
			{
				if (String.IsNullOrEmpty(CtrlReleaseDate.Text))
					return null;
				return Convert.ToDateTime(CtrlReleaseDate.Text);
			}
			set
			{
				CtrlReleaseDate.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 ExpirationDate
		/// </summary>
		public DateTime? ExpirationDate
		{
			get
			{
				if (String.IsNullOrEmpty(CtrlExpirationDate.Text))
					return null;

				return Convert.ToDateTime(CtrlExpirationDate.Text);
			}
			set
			{
				if (value == null)
					CtrlExpirationDate.Text = "";
				else
					CtrlExpirationDate.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 IsMultiSelect
		/// </summary>
		public bool IsMultiSelect
		{
			get
			{
				return Convert.ToBoolean(CtrlIsMultiSelect.Text);
			}
			set
			{
				CtrlIsMultiSelect.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 IsAnonymous
		/// </summary>
		public bool IsAnonymous
		{
			get
			{
				return Convert.ToBoolean(CtrlIsAnonymous.Text);
			}
			set
			{
				CtrlIsAnonymous.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 NeedContactInfo
		/// </summary>
		public bool NeedContactInfo
		{
			get
			{
				return Convert.ToBoolean(CtrlNeedContactInfo.Text);
			}
			set
			{
				CtrlNeedContactInfo.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 UserDefinedTitle
		/// </summary>
		public string UserDefinedTitle
		{
			get
			{
				return Convert.ToString(CtrlUserDefinedTitle.Text);
			}
			set
			{
				if (null != value)
				{
					CtrlUserDefinedTitle.Text = value.ToString();
				}
			}
		}

		/// <summary>
		/// 属性 UserDefinedCount
		/// </summary>
		public int UserDefinedCount
		{
			get
			{
				return Convert.ToInt32(CtrlUserDefinedCount.Text);
			}
			set
			{
				CtrlUserDefinedCount.Text = value.ToString();
			}
		}

		private LoadedValue<List<Module.VoteItem>> loadedVoteItemList;
		/// <summary>
		/// 属性 VoteItemList
		/// </summary>
		public List<Module.VoteItem> VoteItemList
		{
			get
			{
				if (loadedVoteItemList.Loader == null)
					loadedVoteItemList.Loader = () =>
					{
						List<Module.VoteItem> voteItemList = new List<Module.VoteItem>();

						if (!Request.Form.AllKeys.Contains("OptionTitle")) return voteItemList;
						string[] optionTitles = Request.Form.GetValues("OptionTitle");

						for (int i = 0; i < optionTitles.Length; i++)
						{
							int voteCount = IntExtensions.ParseOrDefault(Request.Form.GetValues("VoteCount")[i]);

							voteItemList.Add(new Module.VoteItem() { OptionTitle = optionTitles[i], VoteCount = voteCount });
						}

						return voteItemList;
					};
				return loadedVoteItemList.Value;
			}
			set
			{
				loadedVoteItemList.Value = value;
			}
		}

		public string ChannelIds
		{
			get
			{
				return StringExtensions.CasingCompound<string>(CBLChannelIds.Values, Companies.OperationTypeSeparator);
			}
			set
			{
				if(!string.IsNullOrEmpty(value))
					CBLChannelIds.Values = StringExtensions.CasingDecompound<string>(value, Companies.OperationTypeSeparator);
			}
		}

		/// <summary>
		/// 控件的 VoteInfo 实例。
		/// </summary>
		public Module.Vote Info
		{
			get
			{
				Module.Vote vote = new Module.Vote();

				vote.Title = Title;
				vote.Summary = Summary;
				vote.Content = Content;
				vote.VoteItemList = VoteItemList;
				vote.ReleaseDate = ReleaseDate.Value;
				vote.ExpirationDate = ExpirationDate;
				vote.IsMultiSelect = IsMultiSelect;
				vote.IsAnonymous = IsAnonymous;
				vote.NeedContactInfo = NeedContactInfo;
				vote.UserDefinedTitle = UserDefinedTitle;
				vote.UserDefinedCount = UserDefinedCount;
				vote.ChannelIds = string.IsNullOrEmpty(ChannelIds) ? "" : ChannelIds;

				return vote;
			}
			set
			{
				if (value == null) return;

				Title = value.Title;
				Summary = value.Summary;
				Content = value.Content;
				VoteItemList = value.VoteItemList;
				ReleaseDate = value.ReleaseDate;
				ExpirationDate = value.ExpirationDate;
				IsMultiSelect = value.IsMultiSelect;
				IsAnonymous = value.IsAnonymous;
				NeedContactInfo = value.NeedContactInfo;
				UserDefinedTitle = value.UserDefinedTitle;
				UserDefinedCount = value.UserDefinedCount;
				ChannelIds = value.ChannelIds;
			}
		}
		#endregion
	}
}